package com.woniu.web.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniu.common.entity.TbJpPermission;
import com.woniu.common.entity.TbJpPermissionExample;
import com.woniu.common.entity.TbJpRolePermissionExample;
import com.woniu.common.entity.TbJpRolePermissionKey;
import com.woniu.shiro.pojo.RoleTree;
import com.woniu.shiro.pojo.Trees;
import com.woniu.web.mapper.TbJpPermissionDao;
import com.woniu.web.mapper.TbJpRolePermissionDao;
import com.woniu.web.service.TbJpPermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class TbJpPermissionServiceImpl implements TbJpPermissionService {
    @Resource
    TbJpPermissionDao permissionDao;
    @Resource
    TbJpRolePermissionDao rolePermissionDao;
    @Override
    public List<TbJpPermission> findAllPermission(String title) {
        TbJpPermissionExample permissionExample=new TbJpPermissionExample();
        permissionExample.setOrderByClause("permission_id desc" );
        if (title != null && !title.equals("") && !title.equals("null")) {

            permissionExample.createCriteria().andTitleLike("%" + title + "%");
        }
        return permissionDao.selectByExample(permissionExample);
    }

    @Override
    public void addPermission(TbJpPermission tbJpPermission) {
        permissionDao.insert(tbJpPermission);
    }

    @Override
    public void delPermission(Integer permissionId) {
        permissionDao.deleteByPrimaryKey(permissionId);
    }
    @Override
    public void updatePermission(TbJpPermission tbJpPermission) {
        permissionDao.updateByPrimaryKey(tbJpPermission);
    }

    @Override
    public void delPermissionMore(Integer[] permissionIds) {
        TbJpPermissionExample permissionExample=new TbJpPermissionExample();
        permissionExample.createCriteria().andPermissionIdIn(Arrays.asList(permissionIds));
        permissionDao.deleteByExample(permissionExample);
    }

    @Override
    public Map<String,Object> findAllPermissionTree(Integer roleId) {
        Map<String,Object> maps=new HashMap<>();
        List<Integer> chose=new ArrayList<>();

        List<RoleTree> tree=new ArrayList<>();
        Map<Integer,RoleTree> map=new HashMap<>();
        List<RoleTree> allPermissionTree = permissionDao.findAllPermissionTree();

        TbJpRolePermissionExample permissionExample=new TbJpRolePermissionExample();
        permissionExample.createCriteria().andRoleIdEqualTo(roleId);
        List<TbJpRolePermissionKey> tbJpRolePermissionKeys = rolePermissionDao.selectByExample(permissionExample);

        if (allPermissionTree!=null&&allPermissionTree.size()>0) {
            for (RoleTree roleTree : allPermissionTree) {
               if(roleTree!=null&&roleTree.getId()!=null){
                   map.put(roleTree.getId(),roleTree);
               }

            }
            for (RoleTree roleTree : allPermissionTree) {
                if (roleTree!=null&&roleTree.getParentId()!=null){

                    map.get(roleTree.getParentId()).getChildren().add(roleTree);
                }

            }
            for ( Map.Entry<Integer,RoleTree> m :map.entrySet()){
              if( m!=null&&m.getValue().getParentId()==null){
                 tree.add(m.getValue());
              }

            }
        }
        maps.put("trees",tree);
        if (tbJpRolePermissionKeys!=null&&tbJpRolePermissionKeys.size()>0) {
            for (TbJpRolePermissionKey tbJpRolePermissionKey : tbJpRolePermissionKeys) {
                chose.add(tbJpRolePermissionKey.getPermissionId());
            }
        }
        maps.put("checked",chose);
        return maps;
    }



    @Override
    public void addRolePermission(Integer[] permissionIds) {
        TbJpRolePermissionExample example=new TbJpRolePermissionExample();
        TbJpRolePermissionKey permissionKey=new TbJpRolePermissionKey();

        permissionKey.setRoleId(permissionIds[permissionIds.length-1]);
        example.createCriteria().andRoleIdEqualTo(permissionKey.getRoleId());
        rolePermissionDao.deleteByExample(example);
//        for (int i = 0; i < permissionIds.length-1; i++) {
//
//            permissionKey.setPermissionId(permissionIds[i]);
//            rolePermissionDao.insertRoleAndPermission(permissionKey);
//        }
        List<TbJpRolePermissionKey> list=new ArrayList<>();
        for (int i = 0; i < permissionIds.length-1; i++) {
            TbJpRolePermissionKey permissionKey1=new TbJpRolePermissionKey();
            permissionKey1.setRoleId(permissionIds[permissionIds.length-1]);
            permissionKey1.setPermissionId(permissionIds[i]);

            list.add(permissionKey1);

        }

        rolePermissionDao.insertRoleAndPermission(list);
    }


    @Override
    public List<Trees> UserIdToCheckPermissionByUserId(Integer userId, Integer parentId) {


        return permissionDao.UserIdToCheckPermissionByUserId(userId, parentId);
    }

    @Override
    public List<Trees> UserIdToCheckPermissionByPhone(Long phone) {
        return permissionDao.UserIdToCheckPermissionByPhone(phone);
    }


}
